package stone.ast;

import java.util.List;
import ASTInterpreter.Environment;
import stone.Symbols;


public class Fun extends ASTList {

	protected int size = -1;

	public Fun(List<ASTree> list) {
		super(list);
	}
	
	public ParameterList parameters() {
		return (ParameterList)child(0);
	}
	
	public BlockStmnt body() {
		return (BlockStmnt)child(1);
	}
	
	/*
	public Object eval(Environment env) {
		return new Function(parameters(), body(), env);
	}
	*/

	public void lookup(Symbols symbols) {
		size = lookup(symbols, parameters(), body());
	}

	public static int lookup(Symbols symbols, ParameterList params,
		BlockStmnt body) {
		Symbols newSymbols = new Symbols(symbols);
		params.lookup(newSymbols);
		body.lookup(newSymbols);
		return newSymbols.size();
	}

	public Object eval(Environment env) {
		return new OptFunction(parameters(), body(), env, size);
	}

	
	public String toString() {
		return "( fun " + parameters() + " " + body() + ")";
	}

}
